package org.elsfs.cloud.common.mybatis.ext.sql.support.condition;

import lombok.RequiredArgsConstructor;
import org.elsfs.cloud.common.mybatis.ext.sql.SqlParameterManager;
import org.elsfs.cloud.common.util.lang.NamingCase;

/**
 * 模糊匹配条件项
 *
 */
@RequiredArgsConstructor
public class LikeConditionItem implements ConditionItem {

    /**
     * SQL参数管理器
     */
    protected final SqlParameterManager sqlParameterManager;
    /**
     * 模糊匹配类型
     */
    protected final LikeType likeType;
    /**
     * 左边字段名称
     */
    protected final String leftField;
    /**
     * 模糊匹配值
     */
    protected final String likeValue;

    /**
     * 构建SQL片段
     *
     * @return SQL片段
     */
    @Override
    public String buildSqlFragment() {
        return String.format(
            "%s LIKE %s",
            NamingCase.toUnderlineCase(this.leftField),
            SqlParameterManager.buildParameterPlaceholder(
                this.sqlParameterManager.registerParameter(
                    String.format(
                        this.likeType.getTemplate(),
                        this.likeValue
                    )
                )
            )
        );
    }
}
